package com.gin.no_gay_alliance.fragment.primsg;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.gin.nga.exception.NgaException;
import com.gin.nga.response.body.nuke.PmListBody;
import com.gin.nga.response.field.pm.PrivateMessage;
import com.gin.no_gay_alliance.adpter.pm.PriMsgAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.callback.Callback;
import com.gin.no_gay_alliance.databinding.FragmentPriMsgListBinding;
import com.gin.no_gay_alliance.databinding.MergeRefreshRecyclerViewBinding;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;
import com.gin.no_gay_alliance.listener.MyScrollListener;
import com.gin.no_gay_alliance.manager.PriMsgManager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * 私信列表
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/31 16:43
 **/

public class PriMsgListFragment extends BaseBindingFragment<FragmentPriMsgListBinding> implements Callback<PmListBody>, MyScrollListener.OnScrollToBottomListener, SwipeRefreshLayout.OnRefreshListener {
    private static final String TAG = PriMsgListFragment.class.getSimpleName();
    int page;
    private RecyclerView rv;
    private SwipeRefreshLayout swipeRefreshLayout;
    private PriMsgAdapter adapter;
    private List<PrivateMessage> messages;
    private boolean hasNext;

    @Override
    protected void onCreateView() {
        MergeRefreshRecyclerViewBinding bind = MergeRefreshRecyclerViewBinding.bind(b.getRoot());
        rv = bind.recyclerView;
        swipeRefreshLayout = bind.swipeRefreshLayout;

        MyScrollListener myScrollListener = new MyScrollListener();
        myScrollListener.setOnScrollToBottomListener(this);
        messages = new ArrayList<>();
        adapter = new PriMsgAdapter(requireActivity(), messages);
        rv.setAdapter(adapter);
        rv.setLayoutManager(new LinearLayoutManager(requireActivity(), LinearLayoutManager.VERTICAL, false));
        rv.addOnScrollListener(myScrollListener);
        hasNext = true;
        swipeRefreshLayout.setOnRefreshListener(this);
        // 发送请求
        load(false);
    }


    @Override
    public void onSuccess(PmListBody data) {
        int size = messages.size();
        messages.addAll(data.getData());
        getParentHandler().post(() -> adapter.notifyItemRangeInserted(size, data.getData().size()));
        hasNext = data.isHasNext();
        onLoadFinally();
    }

    @Override
    public void onFailure(@NonNull Call call, @NonNull IOException e) {
        Callback.super.onFailure(call, e);
        onLoadFinally();
    }

    @Override
    public void handleException(NgaException e) {
        Callback.super.handleException(e);
        onLoadFinally();
    }

    /**
     * 已滚动到底部
     */
    @Override
    public void onScrollToBottom() {
        Log.d(TAG, "onScrollToBottom: " + "已滚动到底");
        load(true);
    }

    /**
     * Called when a swipe gesture triggers a refresh.
     */
    @Override
    public void onRefresh() {
        load(false);
    }

    /**
     * 发送请求
     *
     * @param more true 表示为加载更多请求, false表示清空数据,重置到第一页
     */
    private void load(boolean more) {
        if (!hasNext && more) {
            getParentHandler().toast("没有更多数据了");
            return;
        }
        swipeRefreshLayout.setRefreshing(true);
        if (!more) {
            // 清空数据
            messages.clear();
            page = 1;
            adapter.notifyDataSetChanged();
        } else {
            // 否则加载下一页
            page++;
        }
        if (page > 1) {
            getParentHandler().toast("正在加载下一页...");
        }
        PriMsgManager priMsgManager = MyApplication.app.managerFactory.getPriMsgManager();
        priMsgManager.list(page, this);
    }

    private void onLoadFinally() {
        getParentHandler().post(() -> swipeRefreshLayout.setRefreshing(false));
    }
}